package com.dogcatfishdish.depthdisplay 
{
	import com.greensock.easing.Quad;
	import com.greensock.TweenLite;
	
	import flash.display.DisplayObject;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	/**
	 * ...
	 * @author bb
	 */
	public class DragItem extends ShadowDepthItem
	{
		public var zMin:Number = 1;
		public var zMax:Number = 20;
		public var upDuration:Number = 0.1;
		public var downDuration:Number = 0.1;
		public var upEase:Function = Quad.easeOut;
		public var downEase:Function = Quad.easeIn;
		
		public var container:DragContainer;
		public var target:BaseTargetItem;
		
		public var snapToTarget:Boolean = true;
		public var returnToBase:Boolean = true;
		
		protected var initialPosition:Point;
		protected var isDragging:Boolean = false;
		
		public function DragItem(item:DisplayObject) 
		{
			super(item);
			
			buttonMode = useHandCursor = true;
		
			z = zMin;	
			
			addListeners();
		}
		
		protected function addListeners():void {
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 0, true);
			addEventListener(MouseEvent.MOUSE_UP, onMouseUp, false, 0, true);
		}
		
		protected function removeListeners():void {
			removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}
		
		public function setInitialPosition():void {
			initialPosition = new Point(x, y);
		}
		
		protected function miss():void {
			
		}
		
		protected function hit():void {

		}
		
		protected function drop(o:Object):void {
			
			o.z = zMin;
			o.ease = downEase;
			o.onComplete = function():void { isDragging = false; };

			TweenLite.to(this, downDuration, o);
		}
		
		protected function onMouseDown(e:MouseEvent):void {
			
			isDragging = true;
			container.sortDepth(this);
			
			startDrag();
			TweenLite.to(this, upDuration, { z:zMax, ease:upEase } );
		}
		
		
		protected function onMouseUp(e:MouseEvent):void {

			isDragging = false;
			stopDrag();

			if (hitTestObject(target)) {
				hit();
			} else {
				miss();
			}
		}
		
	}

}